/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.badlogic.gdx.Files;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;

import java.util.HashMap;

public class AudioManager extends EngineObject {

    private String basePath="";

    private HashMap<String, Sound> allSounds;
    private HashMap<String, Music> allMusic;

    public AudioManager() {
        this.allSounds = new HashMap<String, Sound>();
        this.allMusic = new HashMap<String, Music>();
    }

    public Sound getSound(String path) {
        return getSound(path, engine.getConfig().getDefaultFileType());
    }

    public Sound getSound(String path, Files.FileType fileType) {
        path = engine.getConfig().getBaseAssetsPath() + basePath + path;  
        String key = createKey(path, fileType);
        Sound sound = allSounds.get(key);
        if (sound == null) {
            sound = engine.application.getAudio().newSound(engine.application.getFiles().getFileHandle(path, fileType));
            allSounds.put(key, sound);
        }
        return sound;
    }

     public Music getMusic(String path) {
        return getMusic(path, engine.getConfig().getDefaultFileType());
     }

    public Music getMusic(String path, Files.FileType fileType) {
        path = engine.getConfig().getBaseAssetsPath() + basePath + path;
        String key = createKey(path, fileType);
        Music music = allMusic.get(key);
        if (music == null) {
            music = engine.application.getAudio().newMusic(engine.application.getFiles().getFileHandle(path, fileType));
            allMusic.put(key, music);
        }
        return music;
    }


    public static String createKey(final String path, final Files.FileType fileType) {
        return new StringBuilder().append(path).append(fileType.ordinal()).toString();
    }

    public void removeSound(String key) {
        if (allSounds.containsKey(key)) {
            allSounds.remove(key);
        } else {
            engine.log("SoundManager", "Unable to remove unknown Sound: " + key);
        }
    }

    public void removeMusic(String key) {
        if (allMusic.containsKey(key)) {
            allMusic.remove(key);
        } else {
            engine.log("SoundManager", "Unable to remove unknown Music: " + key);
        }
    }

    public void removeAllSounds(boolean dispose) {
        for (Sound sound : allSounds.values()) {
            if (dispose)
                sound.dispose();
        }
        allSounds.clear();
    }

    public void removeAllMusic(boolean dispose) {
        for (Music music : allMusic.values()) {
            if (dispose)
                music.dispose();
        }
        allMusic.clear();
    }

    void removeSound(Sound sound, boolean dispose) {
        if (sound == null)
            return;
        if (dispose)
            sound.dispose();
        allSounds.values().remove(sound);
    }

    void removeMusic(Music music, boolean dispose) {
        if (music == null)
            return;
        if (dispose)
            music.dispose();
        allMusic.values().remove(music);
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }
}